package com.example.joblinker.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.joblinker.entity.Resume;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResumeMapper extends BaseMapper<Resume> {
    
    @Select("SELECT * FROM resume WHERE user_id = #{userId}")
    List<Resume> findByUserId(Long userId);
    
    @Select("SELECT * FROM resume WHERE id = #{id} AND user_id = #{userId}")
    Resume findByIdAndUserId(Long id, Long userId);
    
    @Insert("<script>" +
            "INSERT INTO resume (user_id, name, phone, email, " +
            "<if test='age != null'>age,</if> " +
            "gender, education, school, major, " +
            "<if test='graduationYear != null'>graduation_year,</if> " +
            "work_experience, project_experience, skills, certificates, self_evaluation, " +
            "<if test='attachmentUrl != null'>attachment_url,</if> " +
            "status, is_default, " +
            "<if test='title != null'>title,</if> " +
            "<if test='location != null'>location,</if> " +
            "<if test='birthday != null and birthday != \"\"'>birthday,</if> " +
            "<if test='workYears != null'>work_years,</if> " +
            "<if test='jobExpectation != null'>job_expectation,</if> " +
            "<if test='schoolStartDate != null and schoolStartDate != \"\"'>school_start_date,</if> " +
            "<if test='schoolEndDate != null and schoolEndDate != \"\"'>school_end_date,</if> " +
            "<if test='schoolExperience != null'>school_experience,</if> " +
            "<if test='languages != null'>languages,</if> " +
            "create_time, update_time) " +
            "VALUES (#{userId}, #{name}, #{phone}, #{email}, " +
            "<if test='age != null'>#{age},</if> " +
            "#{gender}, #{education}, #{school}, #{major}, " +
            "<if test='graduationYear != null'>#{graduationYear},</if> " +
            "#{workExperience}, #{projectExperience}, #{skills}, #{certificates}, #{selfEvaluation}, " +
            "<if test='attachmentUrl != null'>#{attachmentUrl},</if> " +
            "#{status}, #{isDefault}, " +
            "<if test='title != null'>#{title},</if> " +
            "<if test='location != null'>#{location},</if> " +
            "<if test='birthday != null and birthday != \"\"'>#{birthday},</if> " +
            "<if test='workYears != null'>#{workYears},</if> " +
            "<if test='jobExpectation != null'>#{jobExpectation},</if> " +
            "<if test='schoolStartDate != null and schoolStartDate != \"\"'>#{schoolStartDate},</if> " +
            "<if test='schoolEndDate != null and schoolEndDate != \"\"'>#{schoolEndDate},</if> " +
            "<if test='schoolExperience != null'>#{schoolExperience},</if> " +
            "<if test='languages != null'>#{languages},</if> " +
            "NOW(), NOW())" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Resume resume);
    
    @Update("<script>" +
            "UPDATE resume SET name = #{name}, phone = #{phone}, email = #{email}, " +
            "gender = #{gender}, education = #{education}, school = #{school}, major = #{major}, " +
            "<if test='age != null'>age = #{age},</if> " +
            "<if test='graduationYear != null'>graduation_year = #{graduationYear},</if> " +
            "work_experience = #{workExperience}, project_experience = #{projectExperience}, " +
            "skills = #{skills}, certificates = #{certificates}, self_evaluation = #{selfEvaluation}, " +
            "<if test='attachmentUrl != null'>attachment_url = #{attachmentUrl},</if> " +
            "status = #{status}, " +
            "<if test='title != null'>title = #{title},</if> " +
            "<if test='location != null'>location = #{location},</if> " +
            "<if test='birthday != null and birthday != \"\"'>birthday = #{birthday},</if> " +
            "<if test='workYears != null'>work_years = #{workYears},</if> " +
            "<if test='jobExpectation != null'>job_expectation = #{jobExpectation},</if> " +
            "<if test='schoolStartDate != null and schoolStartDate != \"\"'>school_start_date = #{schoolStartDate},</if> " +
            "<if test='schoolEndDate != null and schoolEndDate != \"\"'>school_end_date = #{schoolEndDate},</if> " +
            "<if test='schoolExperience != null'>school_experience = #{schoolExperience},</if> " +
            "<if test='languages != null'>languages = #{languages},</if> " +
            "update_time = NOW() WHERE id = #{id} AND user_id = #{userId}" +
            "</script>")
    int update(Resume resume);
    
    @Delete("DELETE FROM resume WHERE id = #{id} AND user_id = #{userId}")
    int delete(Long id, Long userId);
    
    @Update("UPDATE resume SET is_default = 0 WHERE user_id = #{userId}")
    int resetDefaultResumeByUserId(Long userId);
    
    @Update("UPDATE resume SET is_default = 1 WHERE id = #{id} AND user_id = #{userId}")
    int setDefaultResume(Long id, Long userId);
} 